package com.doupi.partner.service.impl;

import com.doupi.partner.dal.AdministratorHasRoleMapper;
import com.doupi.partner.dal.model.AdministratorHasRole;
import com.doupi.partner.service.AdministratorHasRoleService;
import com.doupi.partner.util.Pagination;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdministratorHasRoleServiceImpl implements AdministratorHasRoleService {
	private final AdministratorHasRoleMapper administratorHasRoleMapper;

	@Autowired
	public AdministratorHasRoleServiceImpl(AdministratorHasRoleMapper administratorHasRoleMapper) {
		this.administratorHasRoleMapper = administratorHasRoleMapper;
	}

	public int deleteByPrimaryKey(Long id) {
		return administratorHasRoleMapper.deleteByPrimaryKey(id);
	}

	public int insert(AdministratorHasRole record) {
		return administratorHasRoleMapper.insert(record);
	}

	public AdministratorHasRole selectByPrimaryKey(Long id) {
		return administratorHasRoleMapper.selectByPrimaryKey(id);
	}

	public int updateByPrimaryKeySelective(AdministratorHasRole record) {
		return administratorHasRoleMapper.updateByPrimaryKeySelective(record);
	}

	public int countAdministratorHasRole(AdministratorHasRole administratorHasRole) {
		return administratorHasRoleMapper.countAdministratorHasRole(administratorHasRole);
	}

	public List<AdministratorHasRole> queryAdministratorHasRoles(AdministratorHasRole administratorHasRole, RowBounds rowBounds) {
		return administratorHasRoleMapper.queryAdministratorHasRoles(administratorHasRole, rowBounds);
	}

	public List<AdministratorHasRole> selectAdministratorHasRoleForUpdate(AdministratorHasRole administratorHasRole) {
		return administratorHasRoleMapper.selectAdministratorHasRoleForUpdate(administratorHasRole);
	}

	public AdministratorHasRole selectAdministratorHasRoleForUpdateByPrimaryKey(Long id) {
		return administratorHasRoleMapper.selectAdministratorHasRoleForUpdateByPrimaryKey(id);
	}

	public Pagination<AdministratorHasRole, AdministratorHasRole> queryAdministratorHasRoles(Pagination<AdministratorHasRole, AdministratorHasRole> pagination) {
		int count = countAdministratorHasRole(pagination.getParam());
		pagination.setTotalCount(count);
		if (count > 0) {
			pagination.setRecords(queryAdministratorHasRoles(pagination.getParam(), new RowBounds(pagination.getOffset(), pagination.getLimit())));
		}
		return pagination;
	}

	@Override
	public int deleteByAdministratorId(Long administratorId) {
		List<AdministratorHasRole> lists = queryByAdministratorId(administratorId);
		for (AdministratorHasRole temp : lists) {
			deleteByPrimaryKey(temp.getId());
		}
		return 1;
	}

	@Override
	public int deleteByAdministratorRoleId(Long administratorRoleId) {
		AdministratorHasRole administratorToAdministratorRole = new AdministratorHasRole();
		administratorToAdministratorRole.setAdministratorRoleId(administratorRoleId);
		List<AdministratorHasRole> lists = queryAdministratorHasRoles(administratorToAdministratorRole, new RowBounds(0, Integer.MAX_VALUE));
		for (AdministratorHasRole temp : lists) {
			deleteByPrimaryKey(temp.getId());
		}
		return 1;
	}

	@Override
	public List<AdministratorHasRole> queryByAdministratorId(Long administratorId) {
		AdministratorHasRole administratorRole = new AdministratorHasRole();
		administratorRole.setAdministratorId(administratorId);
		return queryAdministratorHasRoles(administratorRole, new RowBounds(0, Integer.MAX_VALUE));
	}
}
